package com.nenude.corejava.ch9.homework;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Judge {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("输入一个邮箱地址：");
		String emailString = scanner.nextLine();
		if (emailString.indexOf("@") == -1 || emailString.indexOf(".") == -1) {
			System.out.println("邮箱地址不包含@或.");
		} else if (emailString.indexOf("@") != emailString.lastIndexOf("@")
				|| emailString.indexOf(".") != emailString.lastIndexOf(".")) {
			System.out.println("邮箱地址包含了多个@或.");
		}else if (emailString.indexOf("@")>emailString.indexOf(".")) {
			System.out.println(".出现在@前面");
		}else if (!checkEmailContent(emailString)) {
			System.out.println("用户名里有其它字符，只能有字母和数字");
		}else {
			System.out.println("有效的邮箱");
		}	
		
		//邮箱正则表达式      ^[0-9a-z]+\w*@([0-9a-z]+\.)[0-9a-z]+$	
		
		String patString="^[0-9a-z]+\\w*@([0-9a-z]+\\.)[0-9a-z]+$"; 
		patString="[a-zA-Z]{1}[0-9a-zA-Z]{7,17}$";
	    Pattern pattern=Pattern.compile(patString);
	    Matcher matcher=pattern.matcher(emailString);
	    if(matcher.matches()){
	        System.out.println("found");
	    }else {
	        System.out.println("not found");
	    }
	    
	}// main
	
	private static boolean checkEmailContent(String email){
		for (int i = 0; i < email.length(); i++) {
			char c=email.charAt(i);
			boolean flag=c>='A'&& c<='Z'||c>='a'&&c<='z'||c=='@'||c=='.';
			if(flag){
				continue;
			}else {
				return false;//只要有一个符号不满足要求，则邮箱不满足要求；
			}
		}	
		return false;
	}
	

}
